package com.src.xyzk_personal.config;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.text.SimpleDateFormat;
import java.util.Date;

//单例日志文件
/**
 * 日志文件
 */
public class LogFile {
	private static  LogFile mlog;
	private String mFilePath;
	private String mSendID;
	private String mRecvID;
	public String m_filename;

	public synchronized static LogFile getInstance(){
		return mlog == null ? mlog = new LogFile() : mlog;
	}
	private LogFile()
	{

	}

	public void startRecord(String name) {
		//按日期创建文件夹
		mFilePath = Common.LogDir + Commonfunc.getDate("") + "/";
		File dir = new File(mFilePath);
		if(!dir.exists()) dir.mkdirs();
		//按时间创建文件 24时制
		m_filename = name + "-" + Commonfunc.getTime("") + ".txt";
		mFilePath += m_filename;
		File file = new File(mFilePath);
		try {
			file.createNewFile();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}


	public long GetLogfileSize() {
		return getFileSize();
	}

	public String GetLogfileName() {
		//去掉LOG标志
		int v_adr = m_filename.indexOf("LOG");
		String v_name = null;
		if (v_adr > 0 && v_adr < 100)
			v_name = m_filename.substring(v_adr + 4);
		return v_name;
	}

	public long readLogfile(char[] Pbuf) {
		return readFile(Pbuf);
	}

	public String getFilePath(){
		return mFilePath;
	}

	public LogFile(String filename,boolean Popen)
	{
		if(Popen == false) return;
		m_filename = Common.LogDir + filename;
	}
	public long getFileSize()
	{
		long v_len = 0;
		File file = new File(mFilePath);
		if(file == null) return v_len;
		//Reader reader = null;
		if(file.exists() && file.isFile())
			v_len = file.length();
		return v_len;
	}
	public long readFile(char [] Pbuf)
	{
		long v_len = 0;
		File file = new File(mFilePath);
		if(file == null) return v_len;
		if(file.exists() && file.isFile())
			v_len = file.length();
		Reader reader = null;
		try {
			reader = new InputStreamReader(new FileInputStream(file));
			v_len = reader.read(Pbuf);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (reader != null) {
			try {
				reader.close();
			} catch (IOException e1) {
			}
		}
		return v_len;
	}
	/**
	 * 记录通讯报文日志
	 * @param canid 1表示发送 2表示接收 3表示7DF
	 * @param data
	 */
	public void printMessage(int canid, String data){
		FileWriter fw;
		String datetime = Commonfunc.getTimeMs();
		try {
			fw = new FileWriter(mFilePath, true);
			if(canid == 1)//发送
				fw.write(datetime + "-->" + mSendID + data + "\n");
			else if(canid == 2)//接收
				fw.write(datetime + "-->" + mRecvID + data + "\n");
			else if(canid == 3)
				fw.write(datetime + "-->" +"7DF:" + data + "\n");
			else if(canid > 100)
				fw.write(datetime + "-->" +String.format("%03X:", canid) + data + "\n");
			else
				fw.write(datetime + "-->"+ data + "\n");
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 读取Log文件
	 * @return 获取上传log文件
	 */
	public String readLogFile(){
		StringBuilder sb = new StringBuilder();
		BufferedReader br;
		try {
			br = new BufferedReader(new FileReader(new File(mFilePath)));
			String line = "";
			while((line = br.readLine()) != null ){
				sb.append(line + "\n");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sb.toString();
	}

	/**
	 * 记录普通日志
	 * @param data普通日志
	 */
	public void printMessage(String data){
		try {
			FileWriter fw = new FileWriter(mFilePath, true);
			fw.write(data);
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public void PrintLog(String Pstr)
	{
		FileWriter fw = null;
		String datetime = "";
		SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd" + " " + "HH:mm:ss");
		Date time = new Date();
		datetime = tempDate.format(time).toString() + "." + Commonfunc.msToString((int) (time.getTime() % 1000));
		try {
			fw = new FileWriter(mFilePath, true);
			fw.write(datetime + "-->" + Pstr + "\n");
			fw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}//
	}
	//写入文件 canid = 1 send   canid = 2 recv   canid = 0 null
	public void PrintAppend(int canid,String data)
	{
		FileWriter fw = null;
		String datetime = "";
		SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd" + " "
				+ "HH:mm:ss");
		Date time = new Date();
		datetime = tempDate.format(time).toString() + "." + Commonfunc.msToString((int) (time.getTime() % 1000));
		try {
			fw = new FileWriter(mFilePath, true);
			if(canid > 10)
			{
				fw.write(datetime + "-->" + Integer.toHexString(canid) + ":"+ data + "\n");
			}
			else{
				fw.write(datetime + "-->"+ data + "\n");
			}
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}//
	}
	public void PrintAppend(String Pmode,int canid,String data)
	{
		FileWriter fw = null;
		String datetime = "";
		SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd" + " " + "HH:mm:ss");
		Date time = new java.util.Date();
		datetime = tempDate.format(time).toString() + "." + Commonfunc.msToString((int) (time.getTime() % 1000));
		try {
			fw = new FileWriter(mFilePath, true);
			fw.write(datetime + "-->" + Commonfunc.canidToHexString(canid) + "(" + Pmode + ")" +":" + data + "\n");
			fw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}//
	}
	public void PrintAppend(String data)
	{
		FileWriter fw = null;
		String datetime = "";
		SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd" + " "
				+ "HH:mm:ss");
		Date time = new Date();
		datetime = tempDate.format(time).toString() + "." + Commonfunc.msToString((int) (time.getTime() % 1000));
		;
		try {
			fw = new FileWriter(mFilePath, true);
			fw.write(datetime + "-->" + Commonfunc.canidToHexString(7 * 0x100 + (0xDF & 0xFF)) + ":" + data + "\n");
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}//
	}
	/**
	 * 设置can ID
	 * @param buf 请求、接收ID字节数组
	 */
	public void setCanId(byte[] buf){
		mSendID = Commonfunc.canidToHexString((buf[0] & 0xFF) * 0x100 + (buf[1] & 0xFF)) + ":";
		mRecvID = Commonfunc.canidToHexString((buf[2] & 0xFF) * 0x100 + (buf[3] & 0xFF)) + ":";
	}
}
